package com.sanno.drugstore.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sanno.common.entity.SccDrugstore;
import com.sanno.drugstore.mapper.DrugstoreMapper;
import com.sanno.drugstore.vo.DrugstoreAndUserVo;
import com.sanno.common.result.Result;
import com.sanno.common.result.ResultCodeEnum;
import com.sanno.drugstore.service.DrugstoreService;
import com.sanno.drugstore.vo.DrugstoreVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/drugstore")
@Api(tags = "药店管理")
@CrossOrigin // 跨域
public class DrugstoreController {

    @Resource
    private DrugstoreService drugstoreServiceImpl;



    @RequestMapping(value = "code",method = RequestMethod.GET)
    @ApiOperation(value = "查询总店信息",notes = "根据code查询总店信息",httpMethod = "GET")
    @ResponseBody
    public Result selectByCode (Integer page,Integer limit ,String searchParams ) throws Exception {

        ObjectMapper objectMapper = new ObjectMapper();
        DrugstoreVo drugstoreVo = null;
        if(searchParams != null){
            drugstoreVo = objectMapper.readValue(searchParams, DrugstoreVo.class);
        }
        Page<DrugstoreAndUserVo> page1 = PageHelper.startPage(page, limit);
        List<DrugstoreAndUserVo> drugstoreAndUserVos = drugstoreServiceImpl.selectByCode(drugstoreVo);
        return Result.ok(drugstoreAndUserVos,page1.getTotal());
    }


    @RequestMapping(value = "queryWrapper",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询分店信息",notes = "根据条件查询分药信息",httpMethod = "GET")
    public Result selectByCode2(Integer page,Integer limit,String searchParams,String store_code1) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        DrugstoreVo drugstoreVo = new DrugstoreVo();
        if(searchParams != null){
             drugstoreVo = objectMapper.readValue(searchParams,DrugstoreVo.class);
        }
        Page<SccDrugstore> page1 = PageHelper.startPage(page, limit);
        List<SccDrugstore> list = drugstoreServiceImpl.selectByCode2(drugstoreVo,store_code1);

        return Result.ok(list,page1.getTotal());
    }

    @RequestMapping(value = "selectStore",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询分店信息",notes = "分店管理员查询",httpMethod = "GET")
    public Result selectByCode3(Integer page,Integer limit,String searchParams,Integer id)throws  Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        DrugstoreVo drugstoreVo = new DrugstoreVo();
        if(searchParams != null){
            drugstoreVo = objectMapper.readValue(searchParams,DrugstoreVo.class);
        }

        Page<SccDrugstore> page1 = PageHelper.startPage(page, limit);
        List<SccDrugstore> list = drugstoreServiceImpl.selectByCode3(drugstoreVo, id);
        return Result.ok(list,page1.getTotal());
    }



    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增药店信息",notes = "新增药店信息",httpMethod = "POST")
    public Result addDrugstore(SccDrugstore sccDrugstore) throws  Exception{

        boolean add = drugstoreServiceImpl.addSccDrugstore(sccDrugstore);
        return Result.ok(ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改分店信息",notes = "根据id分药店信息",httpMethod = "POST")
    public Result updateDrugstore(SccDrugstore sccDrugstore)throws  Exception{

        boolean update = drugstoreServiceImpl.updateById(sccDrugstore);

        return Result.ok(ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "addStoreCode",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "生成药店编码",notes = "生成药店编码",httpMethod = "GET")
    public Result addStoreCode (String storeCode)throws  Exception {

        String[] split = storeCode.split("-",2);
        for (int i =0;i<split.length;i++){
            storeCode = split[0];
        }
        StringBuilder stringBuilder = new StringBuilder(storeCode);
        StringBuilder append = stringBuilder.append("-");
        storeCode = append.toString();
        System.out.println(storeCode);

        List<SccDrugstore> list = drugstoreServiceImpl.addStoreCode(storeCode);

        for (SccDrugstore sccDrugstore : list) {
            storeCode = sccDrugstore.getStoreCode();
        }

        int a;
        String b=null;

        String[] split1 = storeCode.split("-",2);
        for (int i =0;i<split1.length;i++){
              storeCode = split1[1];
              b = split1[0];
        }

        a = Integer.parseInt(storeCode);
        a = a+1;
        if (a > 9 && a <100 ){
            storeCode = b+"-"+"0"+a;
        }else {
            storeCode = b+"-"+"00"+a;
        }
        return Result.ok(storeCode);
    }


}
